package com.jsh.frame.myframe2.wightview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;

/**
 * Created by 王征 on 2017/8/2.
 */

public class CustomMoveView extends CardView {
    private float lastX = 0, lastY = 0;//记录上一次点击  的点的坐标
    private int widthPixels, heightPixels;//手机屏幕的宽、高，用于处理越界

    public CustomMoveView(Context context) {
        super(context);
    }

    public CustomMoveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics dm2 = getResources().getDisplayMetrics();
        widthPixels = dm2.widthPixels;//屏幕宽度
        heightPixels = dm2.heightPixels;//屏幕高度

    }

    public CustomMoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                /**
                 * 记录第一次点击点的坐标
                 * getRawX()、getRawY()触摸点相对屏幕左上角 的位置坐标
                 *
                 **/
                lastX = event.getX();
                lastY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:

                int offsetX = (int) (event.getX() - lastX);//X轴方向上的偏移量
                int offsetY = (int) (event.getY() - lastY);//Y轴方向上的偏移量
                /**
                 *way 1 :
                 *layout(r,t,l,b) 四个参数 左上角 和右下角的坐标
                 * getLeft（）获取此view左边缘到父view的距离
                 * getTop（）获取此view上边缘到父view的距离
                 * getRight（）获取此view右边缘到父view的距离
                 * getBotton（）获取此view下边缘到父view的距离
                 **/
                layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);

                Log.d("ACTION_MOVE ", "lastX=" + lastX + "  lastY=" + lastY);
                Log.d("ACTION_MOVE ", "getX()=" + event.getX() + "  getY()=" + event.getY());
                Log.d("ACTION_MOVE ", "offsetX=" + offsetX + "  offsetY=" + offsetY);

                /**
                 way 2 :
                 **/
//                offsetLeftAndRight(offsetX);
//                offsetTopAndBottom(offsetY);


                break;
            case MotionEvent.ACTION_UP:

                int owenW = getWidth();//自身的长度
                int owenY = getHeight();//自身的高度
                int left = getLeft(), top = getTop(), rignt = getRight(), bottom = getBottom();
//
//                判断是否越界
                if (getLeft() < 0) {
                    left = 0;
                    rignt = owenY;
                }

                if (getTop() < 0) {
                    top = 0;
                    bottom = owenY;

                }

                if (getRight() > widthPixels) {
                    left = widthPixels - owenW;
                    rignt = widthPixels;
                }

                if (getBottom() > heightPixels) {
                    top = heightPixels - owenY;
                    bottom = heightPixels;
                }


                layout(left, top, rignt, bottom);


                break;


        }

        return true;
    }


}
